/**
 * Esta classe e responsavel pela inteligencia do player
 */
 
import corejuliano.ai.*;
import corejuliano.gameelement.agent.*;
import corejuliano.*;
import corejuliano.gameelement.*;
import corejuliano.gameelement.world.*;

import util.*;
import flash.geom.*;

import rocknrollracing.gameelement.agent.*;
 
class rocknrollracing.gameelement.agent.airocknrollracing.PlayerIntelligence extends Intelligence 
{
	private var yAxis:Number = 0;
	private var xAxis:Number = 0;
	private var aceleration:Boolean = false;
	private var timeAceleration:Number = 10;
	private var rotation:Number = 90; 
	
	public function PlayerIntelligence()
	{
		super();
		Key.addListener(this);
		RocknRollRacingAgentModel(owner.getModel()).setRotation(180);
	}
	 	
	public function onKeyDown():Void
	{
		//w = 87
		//d = 68
		// a = 65
		// s = 83
		// esta precionando o "w", para acelerar
		if (Key.getCode() == 87)
		{
			aceleration = true;
			yAxis = -5;
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.NORTH);
		}
		// precionando o "s" para freiar
		if (Key.getCode() == 83)
		{
			aceleration = false;
			yAxis = 5;
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.SOUTH);
		}
		
		if (Key.isDown(Key.LEFT))
		{
			RocknRollRacingAgentModel(owner.getModel()).addRotation(-5);
			xAxis = -5;
			//RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.WEST);
		}
			
		if (Key.isDown(Key.RIGHT))
		{
			RocknRollRacingAgentModel(owner.getModel()).addRotation(5);
			xAxis = 5;
			//RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.EAST);
		}
		
		if (xAxis > 0 && yAxis > 0)
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.SOUTHEAST);
		
		if (xAxis < 0 && yAxis > 0)
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.SOUTHWEST);
		
		if (xAxis < 0 && yAxis < 0)
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.NORTHWEST);
		
		if (xAxis > 0 && yAxis < 0)
			RocknRollRacingAgentModel(owner.getModel()).setDirection(GameManager.NORTHEAST);
		
		clearKeys();
	}
	
	public function onKeyUp():Void
	{
		if (Key.getCode() == 87)
		{
			aceleration = false;
		}
	}
	
	public function updateLogic():Void
	{	
		if(aceleration)
		{
			var x:Number = 0;
			var y:Number = 0;
			
			var direction:Number = RocknRollRacingAgentModel(owner.getModel()).getDirection();
			
			if (direction == GameManager.EAST)
			{
				x = 5;
			}
			else if (direction == GameManager.WEST)
			{
				x = -5;
			}
			else if (direction == GameManager.NORTH)
			{
				y = -5;
			}
			else if (direction == GameManager.SOUTH)
			{
				y = 5;
			}		
			else if (direction == GameManager.SOUTHEAST)
			{
				y = 5;
				x = 5;
			}
			else if (direction == GameManager.SOUTHWEST)
			{
				y = 5;
				x = -5;
			}
			else if (direction == GameManager.NORTHWEST)
			{
				y = -5;
				x = -5;
			}
			else if (direction == GameManager.NORTHEAST)
			{
				y = -5;
				x = 5;
			}	
			
			 
			//var targetPosition:Point = new Point(x, y);
			var targetRotation:Point = calculateAngle(RocknRollRacingAgentModel(owner.getModel()).getRotation());
			Logger.trace("targetRotation: " + targetRotation,1);
			var futurePosition:Point = new Point();
			
			//futurePosition.x = targetRotation.x + targetPosition.x;  
			//futurePosition.y = targetRotation.y + targetPosition.y;
			
			owner.getModel().updatePosition(targetRotation);
		}
		
		
		//RocknRollRacingAgentModel(owner.getModel()).addRotation(targetRotation);			
	}
	
	public function calculateAngle(angle:Number):Point
	{
		var targetAngle:Point = new Point();
		
		targetAngle.x = owner.getModel().getPosition().x * Math.cos(angle) - owner.getModel().getPosition().y * Math.sin(angle);
		targetAngle.y = owner.getModel().getPosition().y * Math.cos(angle) + owner.getModel().getPosition().x * Math.sin(angle);
		
		return targetAngle;
	/*
		Math.cos(x)
		
		xu := xo * cos (ang) - yo * sin (ang)
		yu := yo * cos (ang) + xo * sin (ang) 
	
		xu := (xo-xr) * cos (ang) - (yo-yr) * sin (ang)
		yu := (yo-yr) * cos (ang) + (xo-xr) * sin (ang) 
	*/
		
	}
			
	public function clearKeys():Void
	{
		yAxis = 0;
		xAxis = 0;
	}
}